package Vista;

import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class MenuCargaPatida implements ActionListener{

	private JFrame unFrame;
	private JPanel unPanel;
	
	public MenuCargaPatida(){
		armarPanel();
		incializarFrame();
	}
	
	public MenuCargaPatida(boolean visible){
		armarPanel();
		incializarFrame();
		this.unFrame.setVisible(visible);
	}
	
	public boolean esVisible(){
		return this.unFrame.isVisible();
	}
	
	public void setVisible(boolean b){
		this.unFrame.setVisible(b);	
	}

	private void armarPanel() {
		this.unPanel = new JPanel(new GridLayout(3,1));
		// botones
		JButton botonCargar = new JButton("Cargar");
		final JLabel statusbar = new JLabel("puede cargar"); 
		botonCargar.addActionListener(new ActionListener(){

			@Override
			public void actionPerformed(ActionEvent arg0) {
				JFileChooser chooser = new JFileChooser();
				int opcion = chooser.showOpenDialog(unFrame);
				if (opcion == JFileChooser.APPROVE_OPTION) {
					String algo;
					if (chooser.getSelectedFile()!=null)
						algo = chooser.getSelectedFile().getName();
					else
						algo = "nothing";
					statusbar.setText("You chose " + algo);
					}
					else {
					statusbar.setText("You canceled.");
					}
			}
			
		});
		JButton botonSalir = new JButton("Salir");
		botonSalir.addActionListener(this);
		// agrego botones
		this.unPanel.add(botonCargar);
		this.unPanel.add(botonSalir);
		this.unPanel.add(statusbar);
	}

	private void incializarFrame() {
		this.unFrame = new JFrame();
		this.unFrame.setBounds(50, 50, 175, 275);
		this.unFrame.setContentPane(unPanel);
		this.unFrame.addWindowListener(new MonitorVentanaBasica());
		unFrame.setVisible(true);
		unFrame.setTitle("MenuPrincipal");
		unFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	}
		
	public void actionPerformed(ActionEvent e) {
		if (e.getActionCommand()=="Cagar")
			System.out.println("cargar una partida existente");
		if (e.getActionCommand()=="Salir"){
			this.unFrame.setVisible(false);
		}
			
	}

}
